<template>
  <div class="card-page">
    <tool-bar nav="" :title="action_name + '模型树'">
      <el-button size="small" @click="$router.push('/dev/model-tree-type/add')" v-if="$props.action=='edit'">新建</el-button>
      <el-button size="small" @click="save_click" type="primary">保存</el-button>
    </tool-bar>
    <el-row :gutter="12" style="width: 100%;" justify="space-around">
      <el-col :span="12">
        <el-card shadow="always">
          <template #header>
            <span>基本信息</span>
          </template>
          <kv label="name">
            <el-input v-model="info.name" chk="*" />
          </kv>
          <kv label="remark">
            <el-input v-model="info.remark" type="textarea"/>
          </kv>
          <kv label="parent">
            <ref-parent v-model="info.parent"></ref-parent>
          </kv>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="always">
          <template #header>
            <span>扩展信息</span>
          </template>
        </el-card>
      </el-col>
    </el-row>
    <div>
      <ref-id-name title="parent" v-model="info.parent">
      </ref-id-name>

    </div>
  </div>
</template>

<script setup>
/**
 * Created by CodeGenerator  at 2024-04-15 22:12:27
 */



//定义 vue2的this,当前组件
let $self = getCurrentInstance();

//定义 $attrs
let $attrs = useAttrs()


//定义事件
let $emit = defineEmits(["update:modelValue", "loaded"]);

//定义属性
var $props = defineProps({
  id: {type: String, default: ""},
  action: {type: String, default: ""}
});
const action_name = $computed(() => {
  return {add: "添加", edit: "修改"}[$props.action]
});

//定义data
let $data = reactive({
  info: {
    parent: {},
  }, //子对象需要声明。

})


const info = $computed(() => $data.info)

/**
 * 加载数据
 * @returns {Promise<void>}
 */
const loadData = async ()=> {
  if (!$props.id) return;
  let res = await jv.$http.post("/dev/data-model-tree/detail/" + $props.id)
  $data.info = res.data.data;
}

/**
 * 保存数据
 */
const save_click = async () =>{
  //校验
  if (jv.chk_main() == false) {
    return;
  }

  let res = await jv.$http.post("/dev/data-model-tree/save", $data.info)
  //[axios拦截器中已处理了异常]。
  jv.info(action_name + " 成功");
  let id = res.data.data || $data.info.id;
  if (!id) {
    return jv.error("找不到 id");
  }
  jv.$router.push("/dev/model-tree-type/edit/" + id);
}

</script>

<style scoped lang="scss">
</style>
